package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.global.SQLConf;
import org.jeecg.modules.api.dto.CommentDto;
import org.jeecg.modules.api.vo.comment.CommentVo;
import org.jeecg.modules.music.entity.*;
import org.jeecg.modules.music.mapper.MusCommentMapper;
import org.jeecg.modules.music.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MusCommentServiceImpl extends ServiceImpl<MusCommentMapper, MusComment> implements IMusCommentService {

    @Autowired
    private IMusUserService userService;

    @Autowired
    private IMusCommentService commentService;

    @Autowired
    private IMusSongService songService;

    @Autowired
    private IMusAlbumSongListService albumSongListService;

    @Autowired
    private IMusTopListService topListService;


    @Override
    public IPage<MusComment> getPageList(CommentVo commentVo) {
        QueryWrapper<MusComment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(commentVo.getKeyword())&&!StringUtils.isEmpty(commentVo.getKeyword().trim())){
            queryWrapper.like(SQLConf.CONTENT,commentVo.getKeyword().trim());
        }
        if (commentVo.getType() !=null) {
            queryWrapper.eq(SQLConf.TYPE,commentVo.getType());
        }
        if (StringUtils.isNotEmpty(commentVo.getUsername())) {
            String username = commentVo.getUsername();
            QueryWrapper<MusUser> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.like("nickname", username);
            List<MusUser> list = userService.list(userQueryWrapper);
            if (list.size() > 0) {
                List<String> userUid = new ArrayList<>();
                list.forEach(item -> {
                    userUid.add(item.getId());
                });
                queryWrapper.in(SQLConf.USER_UID, userUid);
            } else {
                // 当没有查询到用户时，默认UID
            }
        }
        if (StringUtils.isNotEmpty(commentVo.getTargetUid())){
            queryWrapper.eq("target_uid",commentVo.getTargetUid());
        }
        Page<MusComment> page = new Page<>();
        page.setCurrent(commentVo.getCurrentPage());
        page.setSize(commentVo.getPageSize());
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<MusComment> pageList = commentService.page(page, queryWrapper);
        List<MusComment> commentList = pageList.getRecords();
        Set<String> userUidSet = new HashSet<>();
        Set<String> targetUidSet = new HashSet<>();
        commentList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getUserUid())) {
                userUidSet.add(item.getUserUid());
            }
            if (StringUtils.isNotEmpty(item.getToUserUid())) {
                userUidSet.add(item.getToUserUid());
            }
            if (StringUtils.isNotEmpty(item.getTargetUid())) {
                targetUidSet.add(item.getTargetUid());
            }
        });
        Map<String,MusSong> songMap = new HashMap<>();
        Map<String,MusAlbumSong> listMap = new HashMap<>();
        Map<String,MusTopList> toplistMap = new HashMap<>();
        // 获取专辑歌单、单曲、榜单
        if (commentVo.getCommentType()!=null){
            if (commentVo.getCommentType()==1){
                Collection<MusSong> songList = new ArrayList<>();
                if (targetUidSet.size()>0) {
                    songList = songService.listByIds(targetUidSet);
                }
                songList.forEach(item -> {
                    // 评论管理并不需要查看博客内容，因此将其排除
                   songMap.put(item.getId(),item);
                });
            }else if (commentVo.getCommentType()==2){
                Collection<MusAlbumSong> list = new ArrayList<>();
                if (targetUidSet.size()>0){
                    list = albumSongListService.listByIds(targetUidSet);

                    list.forEach(item ->{
                        listMap.put(item.getId(),item);
                    });
                }
            }else if (commentVo.getCommentType()==3){
                Collection<MusTopList> topLists = new ArrayList<>();
                if (targetUidSet.size()>0){
                    topLists = topListService.listByIds(targetUidSet);
                    topLists.forEach(item ->{
                        toplistMap.put(item.getId(),item);
                    });
                }
            }
        }
        Collection<MusUser> userCollection = new ArrayList<>();
        if (userUidSet.size() > 0) {
            userCollection = userService.listByIds(userUidSet);
        }
        Map<String, MusUser> userMap = new HashMap<>();
        userCollection.forEach(item -> {
            userMap.put(item.getId(), item);
        });
        // 获取博客
        for (MusComment item : commentList) {
            if (StringUtils.isNotEmpty(item.getUserUid())){
                item.setUser(userMap.get(item.getUserUid()));
            }
            if (StringUtils.isNotEmpty(item.getToUserUid())) {
                item.setToUser(userMap.get(item.getToUserUid()));
            }
            if (StringUtils.isNotEmpty(item.getTargetUid())) {
                if (commentVo.getCommentType()!=null){
                    if (commentVo.getCommentType()==1){
                        item.setSong(songMap.get(item.getTargetUid()));
                    }else if (commentVo.getCommentType()==2){
                        item.setAlbumSong(listMap.get(item.getTargetUid()));
                    }else if (commentVo.getCommentType()==3){
                        item.setTopList(toplistMap.get(item.getTargetUid()));
                    }
                }
            }
        }
        pageList.setRecords(commentList);
        return pageList;
    }

    @Override
    public Boolean addComment(CommentVo commentVo) {
        MusComment comment = new MusComment();
        comment.setTargetUid(commentVo.getTargetUid());
        comment.setContent(commentVo.getContent());
        comment.setUserUid(commentVo.getUserUid());
        comment.setToUid(commentVo.getToUid());
        comment.setToUserUid(commentVo.getToUserUid());
        comment.setCreateTime(new Date());
        comment.setSource(commentVo.getSource());
        comment.setCommentType(commentVo.getCommentType());
        if (baseMapper.insert(comment)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<CommentDto> queryHotComments(String uid, Integer type, Integer limit, Integer offset) {
        List<CommentDto> comments = baseMapper.queryHotComments(uid,type,limit,offset);
        comments.stream().map(CommentDto ->{
            if (StringUtils.isNotEmpty(CommentDto.getUserUid())){
                MusUser user = userService.getById(CommentDto.getUserUid());
                CommentDto.setUser(user);
            }
            return CommentDto;
        }).collect(Collectors.toList());
        comments.stream().map(CommentDto ->{
            if (StringUtils.isNotEmpty(CommentDto.getToUid())){
                List<CommentDto> replyList = baseMapper.queryToUidComments(CommentDto.getToUid());
                replyList.stream().map(comment->{
                    if (StringUtils.isNotEmpty(comment.getUserUid())){
                        MusUser user = userService.getById(comment.getUserUid());
                        comment.setUser(user);
                    }
                    return comment;
                }).collect(Collectors.toList());
                CommentDto.setBeReplied(replyList);
            }
            return CommentDto;
        }).collect(Collectors.toList());
        return comments;
    }

    @Override
    public boolean removeComment(String uid, String userUid, String targetUid) {
        return baseMapper.removeComment(uid,userUid,targetUid);
    }
}
